package com.zzk.audiovideodemo.audiorecorder;

import android.media.AudioFormat;

import com.blankj.utilcode.util.LogUtils;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @ProjectName: AudioVideoDemo
 * @Package: com.zzk.audiovideodemo.audiorecorder
 * @ClassName: Util
 * @Description:
 * @Author: brilliantzhao
 * @CreateDate: 2021.3.23 16:30
 * @UpdateUser:
 * @UpdateDate: 2021.3.23 16:30
 * @UpdateRemark:
 * @Version: 1.0.0
 */

public class Util {

    /**
     *
     * @param fileOutputStream
     * @param audioByteLen
     * @param wavByteLen
     * @param sampleRateInHz
     * @param channelConfig
     * @param audioFormat
     */
    public static void addWavHeader(FileOutputStream fileOutputStream, long audioByteLen, long wavByteLen, int sampleRateInHz, int channelConfig, int audioFormat) {
        byte[] header = new byte[44];

        // RIFF/WAVE header chunk
        header[0] = 'R';
        header[1] = 'I';
        header[2] = 'F';
        header[3] = 'F';
        header[4] = (byte) (wavByteLen & 0xff);
        header[5] = (byte) ((wavByteLen >> 8) & 0xff);
        header[6] = (byte) ((wavByteLen >> 16) & 0xff);
        header[7] = (byte) ((wavByteLen >> 24) & 0xff);

        //WAVE
        header[8] = 'W';
        header[9] = 'A';
        header[10] = 'V';
        header[11] = 'E';

        // 'fmt ' chunk 4 个字节
        header[12] = 'f';
        header[13] = 'm';
        header[14] = 't';
        header[15] = ' ';
        // 4 bytes: size of 'fmt ' chunk（格式信息数据的大小 header[20] ~ header[35]）
        header[16] = 16;
        header[17] = 0;
        header[18] = 0;
        header[19] = 0;
        // format = 1 编码方式
        header[20] = 1;
        header[21] = 0;
        // 声道数目
        int channelSize = channelConfig == AudioFormat.CHANNEL_IN_MONO ? 1 : 2;
        header[22] = (byte) channelSize;
        header[23] = 0;
        // 采样频率
        header[24] = (byte) (sampleRateInHz & 0xff);
        header[25] = (byte) ((sampleRateInHz >> 8) & 0xff);
        header[26] = (byte) ((sampleRateInHz >> 16) & 0xff);
        header[27] = (byte) ((sampleRateInHz >> 24) & 0xff);
        // 每秒传输速率
        long byteRate = audioFormat * sampleRateInHz * channelSize;
        header[28] = (byte) (byteRate & 0xff);
        header[29] = (byte) ((byteRate >> 8) & 0xff);
        header[30] = (byte) ((byteRate >> 16) & 0xff);
        header[31] = (byte) ((byteRate >> 24) & 0xff);
        // block align 数据库对齐单位，每个采样需要的字节数
        header[32] = (byte) (2 * 16 / 8);
        header[33] = 0;
        // bits per sample 每个采样需要的 bit 数
        header[34] = 16;
        header[35] = 0;

        //data chunk
        header[36] = 'd';
        header[37] = 'a';
        header[38] = 't';
        header[39] = 'a';
        // pcm字节数
        header[40] = (byte) (audioByteLen & 0xff);
        header[41] = (byte) ((audioByteLen >> 8) & 0xff);
        header[42] = (byte) ((audioByteLen >> 16) & 0xff);
        header[43] = (byte) ((audioByteLen >> 24) & 0xff);

        LogUtils.d("addWavHeader: " + "header[40]=" + header[40] + "header[41]=" + header[41] + "header[42]=" + header[42] + "header[43]=" + header[43] + " audioByteLen=" + audioByteLen);

        try {
            fileOutputStream.write(header, 0, 44);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
